<?php
function sidebar() {
    global $loc_string;
    global $system;
	global $settings;
?>
		<div class="sidebar-header">
			<div style='float:left;' class='title'><a href='index.php'><?php echo $loc_string['categories_index_title']; ?></a> </div>
			<div class='chats-menu' title='<?php echo $loc_string['tooltip_open_menu']; ?>' onClick='toggleActionMenu();'>☰</div>
		</div>
		<div class="actionmenu">
			<a class='element home' title='<?php echo $loc_string['tooltip_home']; ?>' onClick="loadPage('main');"></a>
			<a class='element' title='<?php echo $loc_string['tooltip_add_category']; ?>' onClick="loadPage('addchat');"></a>
			<a class='element' title='<?php echo $loc_string['tooltip_search']; ?>' onClick="loadPage('search');"></a>
			<a class='element' title='<?php echo $loc_string['tooltip_app_settings']; ?>' onClick="loadPage('appsettings');"></a>
			<a class='element' title='<?php echo $loc_string['tooltip_app_help']; ?>' onClick="loadPage('about_<?php echo $settings['locale']; ?>');"></a>
		</div>
		<?php
    $chats = PDO_FetchAll("SELECT chat.*,count(message.chat_id) as count from chat LEFT JOIN message ON chat_id = chat.id WHERE chat.pinned = 1 GROUP BY chat.id");
    foreach ($chats as $chat) {
?>
        <div title='<?php echo $chat['count'] . " " . $loc_string['tooltip_messages']; ?>' id='<?php echo $chat['id']; ?>' class="contact pinned" onCLick="loadmessages(<?php echo $chat['id']; ?>)">
            <img src="data:image/jpg;base64,<?php echo $chat['image']; ?>" alt="Profile Picture">
            <div class="contact-details">
                <div class="contact-name"><?php echo $chat['name']; ?></div>
                <div class="contact-last-message"><?php echo $chat['desc']; ?></div>
            </div>
        </div>
		<?php
    }
    $chats = PDO_FetchAll("SELECT chat.*,count(message.chat_id) as count from chat LEFT JOIN message ON chat_id = chat.id WHERE chat.pinned IS NOT 1 GROUP BY chat.id");
    foreach ($chats as $chat) {
?>
        <div  title='<?php echo $chat['count'] . " " . $loc_string['tooltip_messages']; ?>' id='<?php echo $chat['id']; ?>' class="contact" onClick="loadmessages(<?php echo $chat['id']; ?>)">
            <img src="data:image/jpg;base64,<?php echo $chat['image']; ?>" alt="Profile Picture">
            <div class="contact-details">
                <div class="contact-name"><?php echo $chat['name']; ?></div>
                <div class="contact-last-message"><?php echo $chat['desc']; ?></div>
            </div>
        </div>
		<?php
    }
?>
		 <div  class="contact" style='font-family:icons; text-align:center; font-size:23px;' title='<?php echo $loc_string['tooltip_add_category']; ?>' onClick="loadPage('addchat')">
          <div style='margin: 0 auto;'> </div>
        </div>
        <?php if (isset($system['first_run']) || isset($_GET['localeset'])): ?>
        <div  class="contact" style='text-align:center; margin:0px, padding:0px; '>
        	<div style='text-align:center; width:100%;'>
        	<span style='font-family:icons;'></span>
          <div style='display:block; clear:both;'><?php echo $loc_string['firstrun_category']; ?></div>
          </div>
        </div>
        <?php
			endif; ?>
		<?php
}
sidebar();
?>
